/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package viewControl;

import entity.Hold;
import entity.Holdfag;
import entity.Laerer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Jeppe Andersen
 */
public class TeacherPanel extends javax.swing.JPanel {
    
    private Session session;
    private ArrayList tempFag;
    private ArrayList laererensFag;
    private ArrayList holdetsFag;

    /**
     * Creates new form TeacherPanel
     */
    public TeacherPanel(Session session) {
        this.session = session;
        tempFag = new ArrayList();
        laererensFag = new ArrayList();
        holdetsFag = new ArrayList();
        initComponents();
    }
    
    protected void update() {

        // update holdne i comboboxen
        Hold h = null;
        if (cbHold.getSelectedIndex() > 0) {
            h = (Hold) cbHold.getSelectedItem();
        }
        cbHold.removeAllItems();
        cbHold.addItem("Hold");
        String hql = "from Hold";
        try {
            Query q = session.createQuery(hql);
            List resultList = q.list();
            for (int i = 0; i < resultList.size(); i++) {
                cbHold.addItem((Hold) resultList.get(i));
            }
        } catch (HibernateException he) {
            System.out.println(he.getLocalizedMessage());
        }
        if (h != null) {
            cbHold.setSelectedItem(h);
        }

        // update lærerne i comboboxen
        Laerer l = null;
        if (cbLaerer.getSelectedIndex() > 0) {
            l = (Laerer) cbLaerer.getSelectedItem();
        }
        cbLaerer.removeAllItems();
        cbLaerer.addItem("Lærer");
        hql = "from Laerer";
        try {
            Query q = session.createQuery(hql);
            Object[] lList = q.list().toArray();
            Arrays.sort(lList);
            for (int i = 0; i < lList.length; i++) {
                cbLaerer.addItem(lList[i]);
            }
        } catch (HibernateException he) {
            System.out.println(he.getLocalizedMessage());
        }
        if (l != null) {
            cbLaerer.setSelectedItem(l);
        }
    }
    
    private void updateHoldfaglisten() {
        holdetsFag = new ArrayList();
        listHoldFag.setListData(holdetsFag.toArray());
        if (cbHold.getSelectedIndex() > 0) {
            Hold h = (Hold) cbHold.getSelectedItem();
            holdetsFag.addAll(h.getHoldfags());
            for (int i = 0; i < laererensFag.size(); i++) {
                Holdfag hf = (Holdfag) laererensFag.get(i);
                if (holdetsFag.contains(hf)) {
                    holdetsFag.remove(hf);
                }
            }
            
            Object[] hfList = holdetsFag.toArray();
            Arrays.sort(hfList);
            holdetsFag = new ArrayList();
            holdetsFag.addAll(Arrays.asList(hfList));
            listHoldFag.setListData(hfList);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnSaveChanges = new javax.swing.JButton();
        btnSletLaerer = new javax.swing.JButton();
        txtLaererLnavn = new javax.swing.JTextField();
        txtLaererFnavn = new javax.swing.JTextField();
        cbLaerer = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        listLaererFag = new javax.swing.JList();
        cbHold = new javax.swing.JComboBox();
        btnTilføjFagHold = new javax.swing.JButton();
        btnFjernFagHold = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listHoldFag = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();

        setLayout(null);

        btnSaveChanges.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnSaveChanges.setText("Gem Ny Lærer");
        btnSaveChanges.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveChangesActionPerformed(evt);
            }
        });
        add(btnSaveChanges);
        btnSaveChanges.setBounds(10, 440, 120, 30);

        btnSletLaerer.setText("Slet");
        btnSletLaerer.setEnabled(false);
        btnSletLaerer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSletLaererActionPerformed(evt);
            }
        });
        add(btnSletLaerer);
        btnSletLaerer.setBounds(170, 440, 51, 30);
        add(txtLaererLnavn);
        txtLaererLnavn.setBounds(70, 100, 150, 20);
        add(txtLaererFnavn);
        txtLaererFnavn.setBounds(70, 60, 150, 20);

        cbLaerer.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Lærer" }));
        cbLaerer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbLaererActionPerformed(evt);
            }
        });
        add(cbLaerer);
        cbLaerer.setBounds(10, 10, 210, 20);

        jScrollPane1.setViewportView(listLaererFag);

        add(jScrollPane1);
        jScrollPane1.setBounds(10, 200, 210, 200);

        cbHold.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Hold", "Item 2", "Item 3", "Item 4" }));
        cbHold.setEnabled(false);
        cbHold.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbHoldActionPerformed(evt);
            }
        });
        add(cbHold);
        cbHold.setBounds(340, 170, 210, 20);

        btnTilføjFagHold.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnTilføjFagHold.setText("<< Tilføj");
        btnTilføjFagHold.setEnabled(false);
        btnTilføjFagHold.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTilføjFagHoldActionPerformed(evt);
            }
        });
        add(btnTilføjFagHold);
        btnTilføjFagHold.setBounds(230, 240, 100, 23);

        btnFjernFagHold.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnFjernFagHold.setText("Fjern >>");
        btnFjernFagHold.setEnabled(false);
        btnFjernFagHold.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFjernFagHoldActionPerformed(evt);
            }
        });
        add(btnFjernFagHold);
        btnFjernFagHold.setBounds(230, 300, 100, 23);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Efternavn");
        add(jLabel2);
        jLabel2.setBounds(10, 100, 60, 20);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Fornavn");
        add(jLabel1);
        jLabel1.setBounds(10, 60, 50, 20);

        jScrollPane2.setViewportView(listHoldFag);

        add(jScrollPane2);
        jScrollPane2.setBounds(340, 200, 210, 200);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Lærerens Fag");
        add(jLabel3);
        jLabel3.setBounds(10, 170, 90, 14);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSletLaererActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSletLaererActionPerformed
        int result = JOptionPane.showConfirmDialog(null, "Er du sikker på du vil slette den valgte lærer?", "Advarsel", 2);
        if (result == JOptionPane.OK_OPTION) {
            Laerer l = (Laerer) cbLaerer.getSelectedItem();
            ArrayList temp = new ArrayList();
            temp.addAll(l.getHoldfags());
            for (int i = 0; i < temp.size(); i++) {
                Holdfag fag = (Holdfag) temp.get(i);
                fag.setLaerer(null);
                session.beginTransaction();
                session.update(fag);
                session.getTransaction().commit();
            }
            session.beginTransaction();
            session.delete(l);
            session.getTransaction().commit();
            update();
        }
    }//GEN-LAST:event_btnSletLaererActionPerformed
    
    private void cbLaererActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbLaererActionPerformed
        laererensFag = new ArrayList();
        cbHold.setSelectedIndex(0);
        if (cbLaerer.getSelectedIndex() > 0) {
            btnSaveChanges.setText("Gem ændringer");
            btnSletLaerer.setEnabled(true);
            btnTilføjFagHold.setEnabled(true);
            btnFjernFagHold.setEnabled(true);
            cbHold.setEnabled(true);
            Laerer l = (Laerer) cbLaerer.getSelectedItem();
            txtLaererFnavn.setText(l.getFornavn());
            txtLaererLnavn.setText(l.getEfternavn());
            laererensFag.addAll(l.getHoldfags());
            
        } else {
            btnSaveChanges.setText("Gem ny lærer");
            btnSletLaerer.setEnabled(false);
            btnTilføjFagHold.setEnabled(false);
            btnFjernFagHold.setEnabled(false);
            cbHold.setEnabled(false);
            txtLaererFnavn.setText("");
            txtLaererLnavn.setText("");
        }
        Object[] lfList = laererensFag.toArray();
        Arrays.sort(lfList);
        laererensFag = new ArrayList();
        laererensFag.addAll(Arrays.asList(lfList));
        listLaererFag.setListData(lfList);
    }//GEN-LAST:event_cbLaererActionPerformed
    
    private void btnSaveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveChangesActionPerformed
        if (txtLaererFnavn.getText().length() > 0) {
            Laerer l;
            if (cbLaerer.getSelectedIndex() > 0) {
                l = (Laerer) cbLaerer.getSelectedItem();
            } else {
                l = new Laerer();
            }
            session.beginTransaction();
            tempFag.addAll(l.getHoldfags());
            for (int i = 0; i < tempFag.size(); i++) {
                Holdfag hf = (Holdfag) tempFag.get(i);
                hf.setLaerer(null);      
            }
            l.getHoldfags().removeAll(l.getHoldfags());
            session.saveOrUpdate(l);
            session.getTransaction().commit();
            session.beginTransaction();
            l.setFornavn(txtLaererFnavn.getText());
            l.setEfternavn(txtLaererLnavn.getText());
            l.getHoldfags().addAll(laererensFag);
            for (int i = 0; i < laererensFag.size(); i++) {
                Holdfag hf = (Holdfag) laererensFag.get(i);
                hf.setLaerer(l);
            }
            session.saveOrUpdate(l);
            session.getTransaction().commit();
            update();
            JOptionPane.showMessageDialog(null, "Læreren er gemt.", "Info", 1);
        } else {
            JOptionPane.showMessageDialog(null, "Læreren skal have et fornavn", "Info", 2);
        }
    }//GEN-LAST:event_btnSaveChangesActionPerformed
    
    private void cbHoldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbHoldActionPerformed
        updateHoldfaglisten();
        
    }//GEN-LAST:event_cbHoldActionPerformed
    
    private void btnTilføjFagHoldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTilføjFagHoldActionPerformed
        if (!listHoldFag.isSelectionEmpty()) {
            laererensFag.add(holdetsFag.get(listHoldFag.getSelectedIndex()));
            Object[] lfList = laererensFag.toArray();
            Arrays.sort(lfList);
            laererensFag = new ArrayList();
            laererensFag.addAll(Arrays.asList(lfList));
            listLaererFag.setListData(lfList);
            updateHoldfaglisten();
        }
    }//GEN-LAST:event_btnTilføjFagHoldActionPerformed
    
    private void btnFjernFagHoldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFjernFagHoldActionPerformed
        if (!listLaererFag.isSelectionEmpty()) {
            laererensFag.remove(laererensFag.get(listLaererFag.getSelectedIndex()));
            
            Object[] lfList = laererensFag.toArray();
            Arrays.sort(lfList);
            laererensFag = new ArrayList();
            laererensFag.addAll(Arrays.asList(lfList));
            listLaererFag.setListData(lfList);
            updateHoldfaglisten();
        }
    }//GEN-LAST:event_btnFjernFagHoldActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFjernFagHold;
    private javax.swing.JButton btnSaveChanges;
    private javax.swing.JButton btnSletLaerer;
    private javax.swing.JButton btnTilføjFagHold;
    private javax.swing.JComboBox cbHold;
    private javax.swing.JComboBox cbLaerer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList listHoldFag;
    private javax.swing.JList listLaererFag;
    private javax.swing.JTextField txtLaererFnavn;
    private javax.swing.JTextField txtLaererLnavn;
    // End of variables declaration//GEN-END:variables
}
